package notify

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
)

func Test_NewMessageBox(t *testing.T) {
	for _, tc := range []struct {
		desc          string
		width         int
		expectedWidth int
	}{
		{
			desc:          "witdh < 72",
			width:         10,
			expectedWidth: 72,
		},
		{
			desc:          "width > =72",
			width:         150,
			expectedWidth: 150,
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			box := NewMessageBox(tc.width)
			require.Equal(t, tc.expectedWidth, box.width)
		})
	}
}

func Test_Draw(t *testing.T) {
	const boxWidth = 72
	for _, tc := range []struct {
		desc           string
		inputs         []string
		expectedOutput string
	}{
		{
			desc:           "input normal line",
			inputs:         []string{"012345678901234567890"},
			expectedOutput: `012345678901234567890`,
		},
		{
			desc:   "long line that exceeds width will be wrapped",
			inputs: []string{strings.Repeat("*", 80)},
			expectedOutput: `************************************************************************
********`,
		},
		{
			desc:   "milti-line input",
			inputs: []string{"**********\n**********\n**********\n"},
			expectedOutput: `**********
**********
**********
`,
		},
		{
			desc:   "non-ascii inputs will be properly processed",
			inputs: []string{"零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九"},
			expectedOutput: `零一二三四五六七八九零一二三四五六七八九零一二三四五六七八九零一二三四五
六七八九零一二三四五六七八九`,
		},
		{
			desc:   "mixed inputs will be properly processed",
			inputs: []string{"0零一二三四五六七八九1零一二三四五六七八九2零一二三四五六七八九3零一二三四五六七八九4零一二三四五六七八九"},
			expectedOutput: `0零一二三四五六七八九1零一二三四五六七八九2零一二三四五六七八九3零一二三
四五六七八九4零一二三四五六七八九`,
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			box := NewMessageBox(boxWidth)
			for _, line := range tc.inputs {
				box.Addf(line)
			}
			require.Equal(t, tc.expectedOutput, box.GetNotify())
		})
	}
}
